package com.heima.controller;



import com.heima.dao.UserDao;
import com.heima.domian.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addUser")
public class AddUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收数据
        String name = req.getParameter("name");
        String age = req.getParameter("age");

       User user =  new User();
       user.setName(name);
       user.setAge(Integer.parseInt(age));
        //调用dao保存数据
        UserDao  userDao = new UserDao();
        //保存方法
        userDao.addUser(user);
        //req.getRequestDispatcher("/success.jsp").forward(req,resp);
        resp.getWriter().write("页面将在2秒后跳转到首页");
        resp.setHeader("Refresh","2;URL="+"/index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
